package com.hz.web.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hz.web.dao.PunishmentDao;
import com.hz.web.entity.DepartmentEntity;
import com.hz.web.entity.PunishmentEntity;
import com.hz.web.service.IdepartmentService;
import com.hz.web.service.IpunishmentService;
/**
 * 处罚公示实现类
 * @author Administrator
 *
 */
@Service
public class PunishmentImpl implements IpunishmentService {

	@Resource
	private PunishmentDao punishmentDao;
	
	@Resource
	private IdepartmentService departmentService;

	/**
	 * 添加处罚公示信息
	 * @param entity
	 * @return
	 */
	@Override
	public int addPunishment(PunishmentEntity entity) {
		int row =-1;
		if(entity!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = sdf.parse(entity.getPunishmentDateStr());
				entity.setPunishmentDate(date);
				row = punishmentDao.addPunishment(entity);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return row;
	}

	/**
	 * 查询处罚信息
	 * @param parmsMap
	 * @return
	 */
	@Override
	public List<PunishmentEntity> queryPunishment(PunishmentEntity entity,
			int page, int rows) {
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		if(entity!=null){
			parmsMap.put("administrativeName", entity.getAdministrativeName());//行政相对人名称
			parmsMap.put("caseName", entity.getCaseName());//案件名称
			parmsMap.put("punishmentOrgan", entity.getPunishmentOrgan());//处罚机关   部门
			parmsMap.put("punishmentDateStart", entity.getPunishmentDateStart());//处罚日期开始时间
			parmsMap.put("punishmentDateEnd", entity.getPunishmentDateEnd());//处罚日期结束时间
			parmsMap.put("isDel", entity.getIsDel());//前台是否显示
		}
		if(page!=0 && rows!=0){
			int index = (page-1)*rows;
			int end =rows; 
			parmsMap.put("index", index);
			parmsMap.put("end", end);
		}
		List<PunishmentEntity> ls =punishmentDao.queryPunishment(parmsMap);
		if(ls==null){
			ls = new ArrayList<PunishmentEntity>();
		}
		
		for (PunishmentEntity punishmentEntity : ls) {
			DepartmentEntity departmentEntity = new DepartmentEntity();
			departmentEntity.setDepartmentCode(punishmentEntity.getPunishmentOrgan());
			List<DepartmentEntity> departmentls = departmentService.queryDepartment(departmentEntity,0,0);
			if(departmentls!=null && departmentls.size()>0){
				punishmentEntity.setPunishmentOrganName(departmentls.get(0).getDepartmentName());
			}else{
				punishmentEntity.setPunishmentOrganName("");
			}
			
		}
		return ls;
	}

	/**
	 * 查询处罚信息总条数
	 * @param parmsMap
	 * @return
	 */
	@Override
	public int queryPunishmentCount(PunishmentEntity entity) {
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		if(entity!=null){
			parmsMap.put("administrativeName", entity.getAdministrativeName());//行政相对人名称
			parmsMap.put("caseName", entity.getCaseName());//案件名称
			parmsMap.put("punishmentOrgan", entity.getPunishmentOrgan());//处罚机关   部门
			parmsMap.put("punishmentDateStart", entity.getPunishmentDateStart());//处罚日期开始时间
			parmsMap.put("punishmentDateEnd", entity.getPunishmentDateEnd());//处罚日期结束时间
			parmsMap.put("isDel", entity.getIsDel());//前台是否显示
		}
		int row =-1; 
		try {
			row = punishmentDao.queryPunishmentCount(parmsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		return row;
	}
}
